class egretToWX extends eui.Component {
	private constructor() {
		super();
	}
	private static instance: egretToWX;
	/**单例 */
	public static getInstance() {
		if (this.instance == null) {
			this.instance = new egretToWX();
		}
		return this.instance;
	}
	private gameHandle;
	/**1 设置舞台*/
	public setGameHandle(game: any) {
		this.gameHandle = game;
		console.log(`stageWidth :${this.gameHandle.stage.stageWidth}   stageHeight:${this.gameHandle.stage.stageHeight}`)
	}
	private closeBtn: eui.Image = new eui.Image();
	private isdisplay = false;
	private bitmap: egret.Bitmap;
	private rankingListMask: egret.Shape;
	/**2 排行榜按钮绑定事件*/
	public paihangBtnClick() {
		SoundManager.getInstance().playEffectSound("click_mp3");
		let openDataContext = wx.getOpenDataContext();
		if (this.isdisplay) {
			this.bitmap.parent && this.bitmap.parent.removeChild(this.bitmap);
			this.rankingListMask.parent && this.rankingListMask.parent.removeChild(this.rankingListMask);
			this.closeBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.paihangBtnClick, this);
			this.closeBtn.parent.removeChild(this.closeBtn);
			this.isdisplay = false;
		} else {

			//处理遮罩，避免开放数据域事件影响主域。
			this.rankingListMask = new egret.Shape();
			this.rankingListMask.graphics.beginFill(0x000000, 1);
			this.rankingListMask.graphics.drawRect(0, 0, this.gameHandle.stage.width, this.gameHandle.stage.height);
			this.rankingListMask.graphics.endFill();
			this.rankingListMask.alpha = 0.5;
			this.rankingListMask.touchEnabled = true;
			this.gameHandle.addChild(this.rankingListMask);
			console.log("调取开放域");

			//简单实现，打开这关闭使用一个按钮。
			//this.gameHandle.addChild(this.gameHandle.paihang);
			//主要示例代码开始
			const bitmapdata = new egret.BitmapData(window["sharedCanvas"]);
			bitmapdata.$deleteSource = false;
			const texture = new egret.Texture();
			texture._setBitmapData(bitmapdata);
			this.bitmap = new egret.Bitmap(texture);
			this.bitmap.width = this.gameHandle.stage.stageWidth;
			this.bitmap.height = this.gameHandle.stage.stageHeight;
			this.gameHandle.addChild(this.bitmap);


			this.closeBtn.texture = RES.getRes("guanbi");
			egret.setTimeout(e => {
				this.gameHandle.addChild(this.closeBtn);
			}, this, 500);

			this.closeBtn.x = this.gameHandle.stage.stageWidth - (this.gameHandle.stage.stageWidth - 840 * this.gameHandle.stage.stageHeight / 640) / 2 - this.closeBtn.width;
			this.closeBtn.y = (this.gameHandle.stage.stageHeight - 590 * this.gameHandle.stage.stageHeight / 640) / 2 + this.closeBtn.height/2;
			this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.paihangBtnClick, this);

			egret.startTick((timeStarmp: number) => {
				egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);
				bitmapdata.webGLTexture = null;
				return false;
			}, this);
			//主要示例代码结束    
			this.isdisplay = true;
		}
		//发送消息
		openDataContext.postMessage({
			isDisplay: this.isdisplay,
			text: 'hello',
			year: (new Date()).getFullYear()
		});
	}
	/**
	 * 3 托管玩家的数据 完成一次游戏之后提交数据 只有提交过数据的玩家才被承认是玩过这个游戏的玩家
	 * KVData参数 例：data=[{key:"score",value:"1234"},{key:"length",value:"1234"},{key:"kill",value:"1234"}]
	 */
	public storageUserData(KVData: any) {
		var object = {
			KVDataList: KVData,
			success: (res: any) => { console.log("托管数据成功 " + res); },
			fail: (res: any) => { console.log("托管数据失败 " + res.Msg); },
			complete: (res: any) => { console.log("完成数据托管操作"); }
		};
		wx.setUserCloudStorage(object);
	}
	/**
	 * 4、根据keyList删除玩家托管的对应数据
	 * keyListData参数 keyListData={["score","length","kill"]}
	 */
	public removeUserStorageData(keyListData: any) {
		var object = {
			keyList: keyListData,
			success: (res: any) => { console.log("删除托管数据成功 " + res); },
			fail: (res: any) => { console.log("删除托管数据失败 " + res.data); },
			complete: (res: any) => { console.log("完成删除数据操作"); }
		};
		wx.removeUserCloudStorage(object);
	}
}
